package com.herohead;

import java.io.ByteArrayInputStream;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
//import java.sql.Statement;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;




public class NWClient {

	public static String clientName;
	public static String clientID;
	public static String networkerVersion;
	public static String licensedApplications;
	public static String scheduledBackup;
	public static String saveSet;
	public static String storageNodes;
	public static String backupType;
	public static String clientDirect;
	public static String backupCommand;
	public static String groups;
	public static String applicationInformation;
	public static String cpus;
	public static String xmlTimeStamp;

	public static void readXML(String nsrClientXML) throws Exception {



		try {
			//String xmlTimeStamp;
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(new ByteArrayInputStream(nsrClientXML.getBytes("UTF-8")));
			doc.getDocumentElement().normalize();

			NodeList nList = doc.getElementsByTagName("nsrClient");
			for (int temp = 0; temp < nList.getLength(); temp++) {
				Node nNode = nList.item(temp);
				if (nNode.getNodeType() == Node.ELEMENT_NODE) {
					Element eElement = (Element) nNode;
					xmlTimeStamp = Misc.checkNull(eElement.getElementsByTagName("timestamp").item(0).getTextContent(),true);
					clientName = Misc.checkNull(eElement.getElementsByTagName("clientName").item(0).getTextContent(),true);
					clientID = Misc.checkNull(eElement.getElementsByTagName("clientID").item(0).getTextContent(),true);
					networkerVersion = Misc.checkNull(eElement.getElementsByTagName("networkerVersion").item(0).getTextContent(),true);
					licensedApplications = Misc.checkNull(eElement.getElementsByTagName("licensedApplications").item(0).getTextContent(),true);
					scheduledBackup = Misc.checkNull(eElement.getElementsByTagName("scheduledBackup").item(0).getTextContent(),true);
					saveSet = Misc.checkNull(eElement.getElementsByTagName("saveSet").item(0).getTextContent(),true);
					storageNodes = Misc.checkNull(eElement.getElementsByTagName("storageNodes").item(0).getTextContent(),true);
					backupType = Misc.checkNull(eElement.getElementsByTagName("backupType").item(0).getTextContent(),false);
					clientDirect = Misc.checkNull(eElement.getElementsByTagName("clientDirect").item(0).getTextContent(),true);
					backupCommand = Misc.checkNull(eElement.getElementsByTagName("backupCommand").item(0).getTextContent(),true);
					groups = Misc.checkNull(eElement.getElementsByTagName("groups").item(0).getTextContent(),true);
					applicationInformation = Misc.checkNull(eElement.getElementsByTagName("applicationInformation").item(0).getTextContent(),true);
					cpus = Misc.checkNull(eElement.getElementsByTagName("cpus").item(0).getTextContent(),false);

				}
				dBuilder.reset();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	Connection connect;
	String SQLConnection = "jdbc:mysql://us01s-netops01.am.mt.mtnet?user=backup&password=networker";




	public static PreparedStatement insertStatement;

	public String createNSRClientXML() throws Exception {
		return this.formatXML(clientName,clientID, networkerVersion, licensedApplications, scheduledBackup, saveSet, storageNodes, backupType, clientDirect, backupCommand, groups, applicationInformation, cpus);

	}

	public String formatXML(String clientName, String clientID,String networkerVersion,String licensedApplications, String scheduledBackup, String saveSet,
			String storageNodes, String backupType, String clientDirect,String backupCommand,String group, String applicationInformation, String cpus ) throws TransformerException{


		String timeStamp = Misc.getTimestamp();

		try {

			DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

			// root elements
			Document doc = docBuilder.newDocument();
			Element rootElement = doc.createElement("nsrClient");
			doc.appendChild(rootElement);

			Element e0 = doc.createElement("timestamp");
			e0.appendChild(doc.createTextNode(timeStamp));
			rootElement.appendChild(e0);

			Element es = doc.createElement("clientName");
			es.appendChild(doc.createTextNode(clientName));
			rootElement.appendChild(es);

			Element clientID1 = doc.createElement("clientID");
			clientID1.appendChild(doc.createTextNode(clientID));
			rootElement.appendChild(clientID1);

			Element es1 = doc.createElement("networkerVersion");
			es1.appendChild(doc.createTextNode(networkerVersion));
			rootElement.appendChild(es1);

			Element e = doc.createElement("licensedApplications");
			e.appendChild(doc.createTextNode(licensedApplications));
			rootElement.appendChild(e);

			Element e1 = doc.createElement("scheduledBackup");
			e1.appendChild(doc.createTextNode(scheduledBackup));
			rootElement.appendChild(e1);

			Element e2 = doc.createElement("saveSet");
			e2.appendChild(doc.createTextNode(saveSet));
			rootElement.appendChild(e2);

			Element e3 = doc.createElement("storageNodes");
			e3.appendChild(doc.createTextNode(storageNodes));
			rootElement.appendChild(e3);

			Element e4 = doc.createElement("backupType");
			e4.appendChild(doc.createTextNode(backupType));
			rootElement.appendChild(e4);

			Element e5 = doc.createElement("clientDirect");
			e5.appendChild(doc.createTextNode(clientDirect));
			rootElement.appendChild(e5);

			Element e6 = doc.createElement("backupCommand");
			e6.appendChild(doc.createTextNode(backupCommand));
			rootElement.appendChild(e6);

			Element e7 = doc.createElement("groups");
			e7.appendChild(doc.createTextNode(groups));
			rootElement.appendChild(e7);

			Element e8 = doc.createElement("applicationInformation");
			e8.appendChild(doc.createTextNode(applicationInformation));
			rootElement.appendChild(e8);

			Element e9 = doc.createElement("cpus");
			e9.appendChild(doc.createTextNode(cpus));
			rootElement.appendChild(e9);


			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			//DOMSource source = new DOMSource(doc);

			//StreamResult result = new StreamResult(new String("soutput"));

			DOMSource source = new DOMSource(doc);
			StreamResult result = new StreamResult(new StringWriter());
			transformer.transform(source, result);
			//System.out.println(result.getWriter().toString());

			return result.getWriter().toString();


		} catch (ParserConfigurationException pce) {
			pce.printStackTrace();
			return pce.toString();
		} catch (TransformerConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return e.toString();
		}

	}


	private int getSequence(String seqName) throws SQLException {

		//System.out.println(SQLConnection);

		connect = DriverManager.getConnection(SQLConnection);
		PreparedStatement seq = connect.prepareStatement("select backup.seq('" + seqName + "') as seq");
		//ResultSet resultSet1 =null;
		ResultSet resultSet1 = seq.executeQuery();
		resultSet1.next();
		String seqNum = resultSet1.getString("seq");

		resultSet1.close();
		seq.close();
		return Integer.parseInt(seqNum);
	}

	public void insertRec() throws Exception{

		/**
		public void insertRec(String clientName, String networkerVersion,String licensedApplications, String scheduledBackup, String saveSet,
				String storageNodes, String backupType, String clientDirect,String backupCommand,String group, String applicationInformation, String cpus ) throws Exception{
		 **/


		int seq = getSequence("nsrClient");
		connect = DriverManager.getConnection(SQLConnection);

		String sqlString = "INSERT INTO backup.nsrclient (idNSRClient,clientName,clientID,networkerVersion,licensedApplications,scheduledBackup,saveSet,storageNodes,backupType,clientDirect,backupCommand,groups,applicationInformation,cpus) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

		insertStatement = connect.prepareStatement(sqlString);
		insertStatement.setInt(1, seq);
		insertStatement.setString(2, clientName);
		insertStatement.setString(3, clientID);
		insertStatement.setString(4, networkerVersion);
		insertStatement.setString(5, licensedApplications);
		insertStatement.setString(6, scheduledBackup);
		insertStatement.setString(7, saveSet);
		insertStatement.setString(8, storageNodes);
		insertStatement.setString(9, backupType);
		insertStatement.setString(10, clientDirect);
		insertStatement.setString(11, backupCommand);
		insertStatement.setString(12, groups);
		insertStatement.setString(13, applicationInformation);
		insertStatement.setString(14, cpus);


		//System.out.println(insertStatement.toString());

		insertStatement.execute();


	}
}